// pages/certificate/index.js
const app = getApp();
const http = require('../../utils/http.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    HTTP: http.ipRoot,
		swiperList: [],
		swiperIndex: 0
	},
	getData(){
		http.post('api/user/my_cert').then(res => {
			if(res.code == 1){
				this.setData(({
					swiperList: res.data
				}))
			}
		})
	},
	
	// 下载图片
	downloadImg() {
		wx.showLoading({
			title: '加载中...'
		});
		//wx.downloadFile方法：下载文件资源到本地
		wx.downloadFile({
			url: this.data.swiperList[this.data.swiperIndex].cert_imgurl, //图片地址
			success: function(res) {
				//wx.saveImageToPhotosAlbum方法：保存图片到系统相册
				wx.saveImageToPhotosAlbum({
					filePath: res.tempFilePath, //图片文件路径
					success: function(data) {
						wx.hideLoading(); //隐藏 loading 提示框
						wx.showModal({
							title: '提示',
							content: '保存成功',
							modalType: false,
						})
					},
					// 接口调用失败的回调函数
					fail: function(err) {
						if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
							err.errMsg === "saveImageToPhotosAlbum:fail auth deny" ||
							err.errMsg ===
							"saveImageToPhotosAlbum:fail authorize no response") {
							wx.showModal({
								title: '提示',
								content: '需要您授权保存相册',
								modalType: false,
								success: modalSuccess => {
									wx.openSetting({
										success(settingdata) {
											console.log(
												"settingdata",
												settingdata)
											if (settingdata
												.authSetting[
													'scope.writePhotosAlbum'
													]) {
												wx.showModal({
													title: '提示',
													content: '获取权限成功,再次点击图片即可保存',
													modalType: false,
												})
											} else {
												wx.showModal({
													title: '提示',
													content: '获取权限失败，将无法保存到相册哦~',
													modalType: false,
												})
											}
										},
										fail(failData) {
											console.log("failData",
												failData)
										},
										complete(finishData) {
											console.log(
												"finishData",
												finishData)
										}
									})
								}
							})
						}
					},
					complete(res) {
						wx.hideLoading(); //隐藏 loading 提示框
					}
				})
			}
		})
	},
	swiperPrev() {
		this.setData({
			swiperIndex: this.data.swiperIndex != 0 ? this.data.swiperIndex - 1 : 0
		})
	},
	swiperNext() {
		this.setData({
			swiperIndex: this.data.swiperIndex < this.data.swiperList.length - 1 ? this.data
				.swiperIndex + 1 : this.data.swiperList.length - 1
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getData();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})